﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _templateprojectajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CheckFormUrl())
            {
                Response.Write("提交有误");
                Response.End();
            }
            Admin_Load("0000", "json");
            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxDel":
                    ajaxDel();
                    break;
                case "ajaxDef":
                    ajaxDef();
                    break;
                case "checkname":
                    ajaxCheckName();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxCheckName()
        {
            if (q("id") == "0")
            {
                doh.Reset();
                doh.ConditionExpress = "title=@title";
                doh.AddConditionParameter("@title", q("txtTitle"));
                if (doh.Exist("jcms_normal_templateproject"))
                    this._response = JsonResult(0, "不可添加");
                else
                    this._response = JsonResult(1, "可以添加");
            }
            else
            {
                doh.Reset();
                doh.ConditionExpress = "title=@title and id<>" + q("id");
                doh.AddConditionParameter("@title", q("txtTitle"));
                if (doh.Exist("jcms_normal_templateproject"))
                    this._response = JsonResult(0, "不可修改");
                else
                    this._response = JsonResult(1, "可以修改");
            }
        }
        private void ajaxGetList()
        {
            doh.Reset();
            doh.SqlCmd = "Select [ID],[Title],[dir],[Info],[IsDefault] FROM [jcms_normal_templateproject] ORDER BY id desc";
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\",returnval :\"操作成功\"," + Jumbot.Common.JsonHelp.DtToSON(dt) + "}";
        }
        private void ajaxDel()
        {
            string cId = f("id");
            doh.Reset();
            doh.ConditionExpress = "pid=@id";
            doh.AddConditionParameter("@id", cId);
            if (doh.Exist("jcms_normal_template"))
            {
                this._response = JsonResult(0, "请先删除方案下的所有模板");
                return;
            }
            doh.Reset();
            doh.ConditionExpress = "id=@id and IsDefault=1";
            doh.AddConditionParameter("@id", cId);
            if (doh.Exist("jcms_normal_templateproject"))
            {
                this._response = JsonResult(0, "默认模板不允许删除");
                return;
            }
            doh.Reset();
            doh.ConditionExpress = "id=@id";
            doh.AddConditionParameter("@id", cId);
            doh.Delete("jcms_normal_templateproject");
            this._response = JsonResult(1, "成功删除");
        }
        private void ajaxDef()
        {
            string cId = f("id");
            doh.Reset();
            doh.AddFieldItem("IsDefault", 0);
            doh.Update("jcms_normal_templateproject");
            doh.Reset();
            doh.ConditionExpress = "id=" + cId;
            doh.AddFieldItem("IsDefault", 1);
            doh.Update("jcms_normal_templateproject");
            doh.Reset();
            doh.AddFieldItem("IsDefault", 0);
            doh.Update("jcms_normal_template");
            doh.Reset();
            doh.ConditionExpress = "pid=" + cId;
            doh.AddFieldItem("IsDefault", 1);
            doh.Update("jcms_normal_template");
            this._response = JsonResult(1, "成功设置");
        }
    }
}